using System;
using System.Text;

namespace MyCollections
{
    public class List<T>
    {
        private T[] _array;
        private int _count;

        public List()
        {
            _array = new T[1];
        }

        public int Count
        {
            get { return _count; }
            //   set { _count = value; }
        }

        public string Print()
        {
            var sb = new StringBuilder();

            for (int i = 0; i < _count; i++)
            {
               sb=sb.Append(_array[i]);
            }
            return sb.ToString();
        }
        
        public void RemoveAt(int index)
        {
            if ((index >= _count) || (index < 0))
            {
                throw new IndexOutOfRangeException();
            }

            for (; index < _count - 1; index++)
            {
                _array[index] = _array[index + 1];
            }
            _count--;
        }

        public void Add(T item)
        {
            if (_array.Length == _count)
            {
                var array2 = new T[_count*2];
                for (int i = 0; i < _count; i++)
                {
                    array2[i] = _array[i];
                }
                _array = array2;
            }
            _array[_count] = item;
            _count++;
        }

        public void Insert(int index, T item)
        {
            if ((index >= _count) || (index < 0))
            {
                throw new IndexOutOfRangeException();
            }

            if (_array.Length == _count)
            {
                var array2 = new T[_count * 2];
                for (int i = 0; i < _count; i++)
                {
                    array2[i] = _array[i];
                }

                _array = array2;
            }

            var tempIndex = index;
            while (index < _count)
            {
                _array[index + 1] = _array[index];
                index++;
            }

            _array[tempIndex] = item;
            _count++;
        }

        public void CopyTo(int index, T[] extArray, int arrayIndex, int quantity)
        {
            if ((index >= _count) || (index < 0))
            {
                throw new IndexOutOfRangeException();
            }

            if (extArray.Length < quantity + arrayIndex)
            {
                throw new IndexOutOfRangeException();
            }

            while (quantity > 0)
            {
                extArray[arrayIndex] = _array[index];

                arrayIndex++;
                index++;
                quantity--;

            }

        }



        public T this[int index]
        {
            get { return _array[index]; }
            set { _array[index] = value; }
        }

        public ListEnumerator<T> GetEnumerator()
        {
            return new ListEnumerator<T>(this); 
        }
    }
}